package com.inspur.edp.metadata.rtcustomization.servermanager.dac.file;

import com.inspur.edp.metadata.rtcustomization.api.entity.GspEmbeddedMdRtContent;
import com.inspur.edp.metadata.rtcustomization.serverapi.MetadataFileService;
import com.inspur.edp.metadata.rtcustomization.servermanager.dac.api.MetadataRtContentBasicRepository;
import com.inspur.edp.metadata.rtcustomization.servermanager.dac.db.MetadataRtContentDbRepositoryImpl;
import com.inspur.edp.metadata.rtcustomization.servermanager.utils.MetadataStorageUtils;
import io.iec.caf.data.jpa.repository.CafI18nColumnDict;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * 元数据仓库文件化存储
 * 将元数据内容存到文件中，其他信息存到H2内存数据库中
 *
 * @author liangff
 * @since 0.1.0
 * 2024/11/23
 */
@Slf4j
public class MetadataRtContentFileRepositoryImpl extends MetadataRtContentDbRepositoryImpl implements MetadataRtContentBasicRepository {
    private static final String RUNTIME_DIRECTORY = "runtime";

    private final MetadataFileService metadataFileService = SpringBeanUtils.getBean(MetadataFileService.class);
    @Override
    public void save(GspEmbeddedMdRtContent gspEmbeddedMdRtContent, CafI18nColumnDict cafI18nColumnDict) {
        if (gspEmbeddedMdRtContent.getSourceType() == 1) {
            metadataFileService.save(gspEmbeddedMdRtContent.getMetadataId(), getPath(), gspEmbeddedMdRtContent.getContent());
            gspEmbeddedMdRtContent.setContent(null);
        }
        super.save(gspEmbeddedMdRtContent, cafI18nColumnDict);
    }

    @Override
    public GspEmbeddedMdRtContent findByMetadataId(String metadataId) {
        GspEmbeddedMdRtContent gspEmbeddedMdRtContent = super.findByMetadataId(metadataId);
        if (gspEmbeddedMdRtContent == null) {
            return null;
        }
        if (gspEmbeddedMdRtContent.getSourceType() != 1) {
            return gspEmbeddedMdRtContent;
        }
        String content = metadataFileService.get(metadataId, getPath());
        if (content == null) {
            log.error("未找到元数据文件：" + metadataId);
            return gspEmbeddedMdRtContent;
        }
        gspEmbeddedMdRtContent.setContent(content);
        return gspEmbeddedMdRtContent;
    }

    @Override
    public GspEmbeddedMdRtContent findByMetadataIdAndSourceType(String metadataId, int sourceType) {
        GspEmbeddedMdRtContent gspEmbeddedMdRtContent = super.findByMetadataIdAndSourceType(metadataId, sourceType);
        if (gspEmbeddedMdRtContent == null) {
            return null;
        }
        if (sourceType != 1) {
            return gspEmbeddedMdRtContent;
        }
        String content = metadataFileService.get(metadataId, getPath());
        if (content == null) {
            log.error("未找到元数据文件：" + metadataId);
            return gspEmbeddedMdRtContent;
        }
        gspEmbeddedMdRtContent.setContent(content);
        return gspEmbeddedMdRtContent;
    }

    @Override
    public void deleteByMetadataId(String metadataId) {
        super.deleteByMetadataId(metadataId);
        metadataFileService.delete(metadataId, getPath());
    }

    private String getPath() {
        return MetadataStorageUtils.INBUILDER_STORAGE_DIRECTORY + File.separator + RUNTIME_DIRECTORY;
    }
}
